/**
 * @file setops.c
 * @author liangwenhao (1943080020@qq.com)
 * @brief 
 * @version 0.1
 * @date 2021-07-29
 * 
 * @copyright Copyright (c) 2021
 * 
 * @authorGitee  https://gitee.com/liangwenhao
 * @authorGithub https://github.com/WHaoL
 * @authorBlogs https://blog.csdn.net/liangwenhao1108
 *  
 * @Code Description: 模拟 sigadd() sigdel() sigismember()的实现  
 *                      信号屏蔽字/阻塞信号集 模拟实现
 *  
 */

#include <signal.h>
#include <errno.h>

/**
 * @brief   <signal.h> usually defines NSIG to include signal number 0.
 *          测试是否是坏数据
 *          NSIG定义了目前系统中信号的最大值
 */
#define SIGBAD(signo) ((signo) <= 0 || (signo) > NSIG)

int sigaddset(sigset_t *set, int signo)
{
    if (SIGBAD(signo))
    {
        errno = EINVAL;
        return -1;
    }

    *set |= (1 << (signo - 1)); /* turn bit on */ //将某信号位 设置为1
}

int sigdelset(sigset_t *set, int signo)
{
    if (SIGBAD(signo))
    {
        errno = EINVAL;
        return -1;
    }

    *set &= ~(1 << (signo - 1)); /* turn bit off */ //将某信号位 设置为0
}

int sigismember(sigset_t *set, int signo)
{
    if (SIGBAD(signo))
    {
        errno = EINVAL;
        return -1;
    }

    return (*set & (1 << (signo - 1)) != 0);
}